package com.dsjy.yf.address.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsjy.yf.address.config.base.BizException;
import com.dsjy.yf.address.config.base.CommonEnum;
import com.dsjy.yf.address.entity.dataobject.YardDO;
import com.dsjy.yf.address.entity.dataobject.YardRoadDO;
import com.dsjy.yf.address.entity.myenum.OperateEnum;
import com.dsjy.yf.address.mapper.YardRoadMapper;
import com.dsjy.yf.address.service.YardRoadService;
import com.dsjy.yf.address.service.YardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author whj
 * @version 1.0
 * @date 2021-01-20 9:16
 */
@Service
public class YardRoadServiceImpl implements YardRoadService {
    @Resource
    private YardRoadMapper yardRoadMapper;
    @Autowired
    private YardService yardService;


    @Override
    @Cacheable(value = "yardroad", condition = "#flag == true", key = "'searchYardRoadWithYardId'+#parentCode")
    public List<YardRoadDO> searchYardRoadWithParentCode(Integer parentCode, Boolean flag) {
        return yardRoadMapper.searchYardRoadWithParentCode(parentCode, flag);
    }

    @Override
    @CacheEvict(value = "yardroad", allEntries = true)
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer addUpdDel(String operate, YardRoadDO yardRoadDO) {
        Integer result = null;
        if (OperateEnum.ADD.getOperate().equals(operate) && yardRoadDO.getYardroadName() != null) {
            result = addYardRoad(yardRoadDO);
        } else if (OperateEnum.DELETE.getOperate().equals(operate) && yardRoadDO.getId() != null) {
            result = delYardRoad(yardRoadDO.getId());
        } else if (OperateEnum.UPDATE.getOperate().equals(operate) && yardRoadDO.getId() != null && yardRoadDO.getParentCode() != null) {
            result = updYarRoad(yardRoadDO);
        }
        return result;
    }

    @Override
    public Integer addYardRoad(YardRoadDO yardRoadDO) {
        Integer yardId = yardRoadDO.getParentCode();
        if (yardId == null) {
            throw new BizException(CommonEnum.PARAM_ERROR);
        }
        YardDO yardDO = yardService.searchYardWithId(yardId);
        if (yardDO != null && yardDO.getIsHaveRoad() == 0) {
            yardDO.setIsHaveRoad(1);
            yardDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            yardService.addUpdDel("upd", yardDO);
        }
        yardRoadDO.setCreateTime(new Timestamp(System.currentTimeMillis()));
        return yardRoadMapper.addYardRoad(yardRoadDO);
    }

    @Override
    public Integer delYardRoad(Integer id) {
        YardRoadDO yardRoadDO = yardRoadMapper.selectById(id);
        Integer result = yardRoadMapper.deleteById(id);
        List<YardRoadDO> list = yardRoadMapper.searchYardRoadWithParentCode(yardRoadDO.getParentCode(), false);
        if (list.size() == 0) {
            YardDO yardDO = new YardDO();
            yardDO.setId(yardRoadDO.getParentCode());
            yardDO.setIsHaveRoad(0);
            yardDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            yardService.addUpdDel("upd", yardDO);
        }
        return result;
    }

    @Override
    @CacheEvict(value = "yardroad", condition = "#parentCode != null", key = "'searchYardRoadWithYardId'+#parentCode")
    @Transactional(rollbackFor = RuntimeException.class)
    public Integer delYardRoadByParentCode(Integer parentCode) {
        return yardRoadMapper.delYardRoadByParentCode(parentCode);
    }

    @Override
    public Integer updYarRoad(YardRoadDO yardRoadDO) {
        yardRoadDO.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        return yardRoadMapper.updYarRoad(yardRoadDO);
    }

    @Override
    public IPage<YardRoadDO> searchWithPage(Integer pageNum, Integer pageSize, QueryWrapper wrapper) {
        wrapper.setEntityClass(YardRoadDO.class);
        Page<YardRoadDO> page = new Page<>(pageNum, pageSize);
        return yardRoadMapper.selectPage(page, wrapper);
    }
}
